# 服务端
# 由blender运行
import socket
from threading import Thread
from queue import Queue
address = "127.0.0.1"
port = 6999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2.绑定套接字
server.bind((address, port))  # 绑定要监听的端口
# 3.监听套接字
server.listen(5)  # 开始监听 表示可以使用五个链接排队


class Conn:
    cmd = Queue()
    value = Queue()

def run_server():
    while True:
        conn, _ = server.accept()
        while True:
            try:
                data = conn.recv(1024)  # 接收数据
                if not data:
                    break
                cmd = data.decode()
                Conn.cmd.put(cmd)
                value = Conn.value.get()
                print('recive:', value)  # 打印接收到的数据
                conn.send(str(value).encode())  # 然后再发送数据
            except ConnectionResetError as e:
                print('关闭了正在占线的链接！')
                print(e.args)
                break
        conn.close()

Thread(target=run_server, daemon=True).start()